<?php

namespace App\Extensions\Actions\Post;

use App\Extensions\Actions\RowAction;
use App\Models\AdminUser;
use App\Patent;
use App\PatentApply;
use App\Services\PatentService;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class GrantPatentPost extends RowAction
{
    public $name = '已授权';

    public function handle(PatentApply $model, Request $request)
    {
        $user = Admin::user();
        if ($user['role_type'] == 1) {
            $model->status = 1;
        }
        $patent_type_id = $request->get('_patent_type_id');
        $patent_sn = $request->get('patent_sn');
        $publication_num = $request->get('publication_num');
        $patent_sn = PatentService::check_patent_sn($patent_sn, $patent_type_id);
        $res = PatentApply::query()->where('patent_sn', $patent_sn)->first();
        if ($res && $res['id'] != $model['id']) {
            return $this->response()->swal()->error('此专利已存在，不能重复添加！')->refresh();
        }
        if ($model['other_applicant']) {
            $applicant = $model['patent_person'] . ',' . $model['other_applicant'];
        } else {
            $applicant = $model['patent_person'];
        }
        $data = [
            'patent_apply_id'  => $model['id'],
            'patent_sn'        => $patent_sn,
            'apply_date'       => $request->get('apply_date'),
            'publication_num'  => $publication_num,
            'publication_date' => $request->get('publication_date'),
            'patent_name'      => $request->get('patent_name') ?: $model['patent_name'],
            'patent_type_id'   => $patent_type_id,
            'patent_person'    => $model['patent_person'],
            'applicant'        => $request->get('applicant') ?: $applicant,
            'inventor'         => $request->get('inventor') ?: $model['inventor'],
            'user_id'          => $model['user_id'],
            'department_id'    => $model['department_id'],
            'legal_status'     => '已授权',
        ];
        DB::beginTransaction();
        try {
            if ($model->patent()->first()) {
                $model->patent()->update($data);
            } else {
                $patent = Patent::query()->where('patent_apply_id', 0)->where('patent_sn', $patent_sn)->first();
                if ($patent) {
                    $patent->update($data);
                } else {
                    $model->patent()->create($data);
                }
            }
            $model->patent_type_id = $patent_type_id;
            $model->patent_sn = $patent_sn;
            $model->apply_date = $request->get('apply_date');
            $model->publication_num = $publication_num;
            $model->publication_date = $request->get('publication_date');
            $model->save();
            if ($file = $request->file('cert')) {
                $model->storeFile($file, 'cert');
            }
            DB::commit();
        } catch (\Exception $exception) {
            DB::rollBack();
            return $this->response()->swal()->success('操作失败')->refresh();
        }
        return $this->response()->swal()->success('操作成功')->refresh();
    }

    public function form()
    {
        $row = $this->row;
        $this->radio('_patent_type_id', __('专利类型'))->options(patent_type_select_option())->default($row->patent_type_id)->required();
        $this->text('patent_name', __('Patent name'))->default($row->patent_name);
        $this->date('apply_date', __('Apply date'))->required()->default($row->apply_date);
        $this->text('patent_sn', __('Patent sn'))->rules('required|regex:/^[a-zA-Z0-9X\.]+$/', [
            'regex'    => __('Patent sn') . '组成：只能为字母、数字、点',
            'required' => __('Patent sn') . '不能为空',
        ])->default($row->patent_sn);
        $this->date('publication_date', __('公告日'))->required()->default($row->publication_date);
        $this->text('publication_num', __('公告号'))->required()->default($row->publication_num);
        //$this->text('legal_status', __('legal_status'));
//        $this->text('applicant', __('Applicant'))->default($applicant);
//        $this->text('inventor', __('inventor'))->default($model->inventor);
        $this->file('cert', '上传证书')->hidePreview();
    }

    public function authorize(AdminUser $user, $model): bool
    {
        return $user->role_type == 1 || $user['id'] == $model['user_id'];
    }


}
